#include "StdAfx.h"
#include "DrDatajointMag.h"
#include "LogWrapper.h"


CDrDatajointMag::CDrDatajointMag(void)
{
	m_hModule = NULL;
	m_lpDrSelectPos = NULL;
	m_lpTspSelectPos= NULL;
}


CDrDatajointMag::~CDrDatajointMag(void)
{
	this->Destroy();
}

bool CDrDatajointMag::Initialize()
{
#ifndef NOT_USE_DATA_TRANSMIT_DLL
#ifdef _WIN64
	m_hModule = LoadLibrary(L"SelectPos64.dll");
#else
	m_hModule = LoadLibrary(L"SelectPos64.dll");
#endif
	DWORD dwError = GetLastError();
	if (dwError != 0 && !m_hModule)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"load SelectPos failed. Error code is %d.\n", dwError);
		return false;
	}
	m_lpDrSelectPos = (LPDRSelectPos)GetProcAddress(m_hModule, "DRSelectPos");
	dwError = GetLastError();
	if (!m_lpDrSelectPos)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"get DR Select Pos interface failed. Error code is %d.\n", dwError);
		return false;
	}
	m_lpTspSelectPos = (LPTSPSelectPos)GetProcAddress(m_hModule, "TSPSelectPos");
	dwError = GetLastError();
	if (!m_lpDrSelectPos)
	{
		CSingleton<CLogWrapper>::Instance()->Log(ERROR_LOG, L"get TSP Select Pos interface failed. Error code is %d.\n", dwError);
		return false;
	}
#endif
	return true;
}

Pos CDrDatajointMag::TspSelectPos(int w, int nSampleNum, WORD * sampleH, WORD * sampleL, unsigned long * pSampleConveyer, int nDatasetNum, WORD * DatasetH, WORD * DatasetL, unsigned long * pDatasetConveyer )
{
	if (m_lpTspSelectPos)
	{
		return (*m_lpTspSelectPos)(w,nSampleNum,sampleH,sampleL,pSampleConveyer,nDatasetNum,DatasetH,DatasetL,pDatasetConveyer);
	}
	else
	{
		Pos pos;
		pos.DatasetPos=-1;
		pos.DatasetPos=-1;
		return pos;
	}
}

Pos CDrDatajointMag::DrSelectPos( int h, int nSampleNum, xray_pixel * sample, unsigned long * pSampleConveyer, int nDatasetNum, xray_pixel * dataset, unsigned long * pDatasetConveyer )
{
	if (m_lpDrSelectPos)
	{
		return (*m_lpDrSelectPos)(h,nSampleNum,sample,pSampleConveyer,nDatasetNum,dataset,pDatasetConveyer);
	}
	else
	{
		Pos pos;
		pos.DatasetPos=-1;
		pos.DatasetPos=-1;
		return pos;
	}
}

void CDrDatajointMag::Destroy()
{
	if (m_hModule)
	{
		FreeLibrary(m_hModule);
		m_lpDrSelectPos = NULL;
		m_lpTspSelectPos= NULL;
	}
}
